package com.gzyj.flink.dto.alarm;

import com.gzyj.flink.dto.utils.DateUtils;
import com.gzyj.flink.dto.utils.ToolUtils;
import com.gzyj.flink.gps.GPSPosition;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

@Data
public class FatigueDrivingData implements Serializable {

    /// <summary>
    /// 不受Reset控制
    /// </summary>
    public Date LocalTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 不受Reset控制
    /// </summary>
    public Date GpsTime = DateUtils.MIN_VALUE();

    /// <summary>
    /// 判断车辆数据是否开始计算
    /// </summary>
    public boolean IsCalc = false;
    /// <summary>
    /// 计算开始时间状态(日间：false，夜间：true）
    /// </summary>
    public boolean IsCalcInNight = false;
    /// <summary>
    /// 满足条件开始时间
    /// </summary>
    public Date ConditionBeginTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 满足条件开始时经度
    /// </summary>
    public double ConditionBeginLongitude = 0;
    /// <summary>
    /// 满足条件开始时纬度
    /// </summary>
    public double ConditionBeginLatitude = 0;
    /// <summary>
    /// 满足条件结束时间
    /// </summary>
    public Date ConditionEndTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 满足条件结束时经度
    /// </summary>
    public double ConditionEndLongitude = 0;
    /// <summary>
    /// 满足条件结束时纬度
    /// </summary>
    public double ConditionEndLatitude = 0;

    /// <summary>
    /// GUID
    /// 单独生成 不受Reset控制
    /// </summary>
    public String SingleGuid = UUID.randomUUID().toString();

    /// <summary>
    /// 是否正在休息
    /// </summary>
    public boolean IsRest = false;
    /// <summary>
    /// 休息开始时间状态(日间：false，夜间：true）
    /// </summary>
    public boolean IsRestInNight = false;
    /// <summary>
    /// 休息开始时间
    /// </summary>
    public Date RestBeginTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 休息结束时间
    /// </summary>
    public Date RestEndTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 是否正在报警
    /// </summary>
    public boolean IsAlarm = false;
    /// <summary>
    /// 报警开始时间
    /// </summary>
    public Date AlarmBeginTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 报警开始时经度
    /// </summary>
    public double AlarmBeginLongitude = 0;
    /// <summary>
    /// 报警开始时纬度
    /// </summary>
    public double AlarmBeginLatitude = 0;
    /// <summary>
    /// 报警结束时间
    /// </summary>
    public Date AlarmEndTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 报警结束时经度
    /// </summary>
    public double AlarmEndLongitude = 0;
    /// <summary>
    /// 报警结束时纬度
    /// </summary>
    public double AlarmEndLatitude = 0;
    /// <summary>
    /// 报警持续过程中的最大速度
    /// </summary>
    public int AlarmMaxSpeed = 0;
    /// <summary>
    /// 报警持续里程
    /// </summary>
    public double AlarmDistance = 0;
    /// <summary>
    /// 报警等级
    /// </summary>
    public int AlarmLevel = 1;
    /// <summary>
    /// 最后发送预警短信得时间
    /// </summary>
    public Date WarningContentSendTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 发送预警短信得次数
    /// </summary>
    public int WarningContentCount = 0;
    /// <summary>
    /// 最后发送报警短信得时间
    /// </summary>
    public Date AlarmContentSendTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 发送报警短信得次数
    /// </summary>
    public int AlarmContentCount = 0;
    /// <summary>
    /// 最后的的驾驶员
    /// </summary>
    public String DriverNo = "";

    /// <summary>
    /// 1默认规则 2=5级规则 3=新疆出租车 4=新疆营运车
    /// </summary>
    public int AlarmSetting = 0;

    /// <summary>
    /// 计算计算时间差，返回以秒为单位的结果，如2323.5秒
    /// </summary>
    public double CalcConditionTimeSpan() {

        long ts = this.ConditionEndTime.getTime() - this.ConditionBeginTime.getTime();
        return ts / 1000;

    }

    /// <summary>
    /// 开始报警
    /// </summary>
    public void StartAlarm(GPSPosition gps) {
        this.IsAlarm = true;
        this.AlarmBeginTime = gps.getGPSTimeExt();
        this.AlarmBeginLongitude = gps.getLngExt();
        this.AlarmBeginLatitude = gps.getLatExt();
        this.AlarmEndTime = gps.getGPSTimeExt();
        this.AlarmEndLongitude = gps.getLngExt();
        this.AlarmEndLatitude = gps.getLatExt();
        this.AlarmMaxSpeed = gps.getSpeedExt();
        this.AlarmDistance = 0;
        //this.AlarmLevel = 1;
    }

    /// <summary>
    /// 结束报警
    /// </summary>
    public void StopAlarm(GPSPosition gps, String customAlarmEndTime) {
        this.IsAlarm = false;
        double lon = this.AlarmEndLongitude;
        double lat = this.AlarmEndLatitude;
        if (StringUtils.isEmpty(customAlarmEndTime)) {
            this.AlarmEndTime =gps.getGPSTimeExt();
        } else {
            if (DateUtils.toDate(customAlarmEndTime) == DateUtils.MIN_VALUE()) {
                this.AlarmEndTime = gps.getGPSTimeExt();
            } else {
                this.AlarmEndTime = DateUtils.toDate(customAlarmEndTime).getTime() - gps.getGPSTimeExt().getTime() > 0 ? gps.getGPSTimeExt() : DateUtils.toDate(customAlarmEndTime);
            }
        }
        this.AlarmEndLongitude = gps.getLngExt();
        this.AlarmEndLatitude = gps.getLatExt();
        this.AlarmMaxSpeed = Math.max(this.AlarmMaxSpeed,gps.getSpeedExt());
        this.AlarmDistance = this.AlarmDistance + ToolUtils.GetDistance(lon, lat, gps.getLngExt(), gps.getLatExt());
    }


    /// <summary>
    /// 记录报警过程中的数据
    /// </summary>
    public void WriteAlarm(GPSPosition gps) {
        double lon = this.AlarmEndLongitude;
        double lat = this.AlarmEndLatitude;
        this.AlarmEndTime = gps.getGPSTimeExt();
        this.AlarmEndLongitude = gps.getLngExt();
        this.AlarmEndLatitude = gps.getLatExt();
        this.AlarmMaxSpeed = Math.max(this.AlarmMaxSpeed, gps.getSpeedExt());
        this.AlarmDistance = this.AlarmDistance + ToolUtils.GetDistance(lon, lat, gps.getLngExt(), gps.getLatExt());
    }

    /// <summary>
    /// 重置计算数据
    /// </summary>
    public void ResetData() {
        this.IsCalc = false;
        this.IsCalcInNight = false;
        this.ConditionBeginTime = DateUtils.MIN_VALUE();
        this.ConditionBeginLongitude = 0;
        this.ConditionBeginLatitude = 0;
        this.ConditionEndTime = DateUtils.MIN_VALUE();
        this.ConditionEndLongitude = 0;
        this.ConditionEndLatitude = 0;

        this.IsRest = false;
        this.IsRestInNight = false;
        this.RestBeginTime = DateUtils.MIN_VALUE();
        this.RestEndTime = DateUtils.MIN_VALUE();

        this.IsAlarm = false;
        this.AlarmBeginTime = DateUtils.MIN_VALUE();
        this.AlarmBeginLongitude = 0;
        this.AlarmBeginLatitude = 0;
        this.AlarmEndTime = DateUtils.MIN_VALUE();
        this.AlarmEndLongitude = 0;
        this.AlarmEndLatitude = 0;
        this.AlarmMaxSpeed = 0;
        this.AlarmDistance = 0;
        this.AlarmLevel = 1;
        this.WarningContentSendTime = DateUtils.MIN_VALUE();
        this.WarningContentCount = 0;
        this.AlarmContentSendTime = DateUtils.MIN_VALUE();
        this.AlarmContentCount = 0;

        this.AlarmSetting = 0;
    }

    /// <summary>
    /// 开始计算
    /// </summary>
    public void StartCalc(GPSPosition gps, String nightStartTime, String nightEndTime) {
        this.IsCalc = true;
        this.IsCalcInNight = DateUtils.InTimeRange(gps.getTIME(), nightStartTime, nightEndTime);
        this.ConditionBeginTime = gps.getGPSTimeExt();
        this.ConditionBeginLongitude = gps.getLngExt();
        this.ConditionBeginLatitude = gps.getLatExt();
        this.ConditionEndTime = gps.getGPSTimeExt();
        this.ConditionEndLongitude = gps.getLngExt();
        this.ConditionEndLatitude = gps.getLatExt();
    }

    /// <summary>
    /// 记录计算过程中的数据
    /// </summary>
    public void WriteCalc(GPSPosition gps) {
        this.ConditionEndTime = gps.getGPSTimeExt();
        this.ConditionEndLongitude = gps.getLngExt();
        this.ConditionEndLatitude = gps.getLatExt();
    }

    public void GenerateSingleGuid() {
        this.SingleGuid = UUID.randomUUID().toString();
    }

    /// <summary>
    /// 计算休息时间差，返回以秒为单位的结果，如2323.5秒
    /// </summary>
    public long CalcRestTimeSpan() {
        long ts = (RestEndTime.getTime() - RestBeginTime.getTime()) / 1000;
        return ts;

    }

    /// <summary>
    /// 重置休息数据
    /// </summary>
    public void ResetRest() {
        this.IsRest = false;
        this.IsRestInNight = false;
        this.RestBeginTime = DateUtils.MIN_VALUE();
        this.RestEndTime = DateUtils.MIN_VALUE();
    }

    /// <summary>
    /// 开始休息
    /// </summary>
    public void StartRest(GPSPosition gps, String nightStartTime, String nightEndTime) {
        this.IsRest = true;
        this.IsRestInNight = DateUtils.InTimeRange(gps.getTIME(), nightStartTime, nightEndTime);
        this.RestBeginTime = gps.getGPSTimeExt();
        this.RestEndTime = gps.getGPSTimeExt();
    }

    /// <summary>
    /// 记录休息过程中的数据
    /// </summary>
    public void WriteRest(GPSPosition gps) {
        this.RestEndTime = gps.getGPSTimeExt();
    }


}
